package com.jewelry.web.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jewelry.common.util.Constants;
import com.jewelry.service.IUserService;
import com.jewelry.vo.StatisticsVO;
import com.jewelry.vo.UserVO;

/**
 * 用户管理
 * 
 * @author yangfeng
 * 
 */
public class UserAction extends HttpAction {

	private static final long serialVersionUID = -1661851861944440831L;
	
	private static Logger logger = LoggerFactory.getLogger(UserAction.class);

	// ======================================
	private UserVO user;

	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public UserVO getUser() {
		return user;
	}

	public void setUser(UserVO user) {
		this.user = user;
	}

	public List<StatisticsVO> statisticsList;

	public List<StatisticsVO> getStatisticsList() {
		return statisticsList;
	}

	public void setStatisticsList(List<StatisticsVO> statisticsList) {
		this.statisticsList = statisticsList;
	}

	private List<UserVO> userList;

	public void setUserList(List<UserVO> userList) {
		this.userList = userList;
	}

	public List<UserVO> getUserList() {
		return userList;
	}

	// ==============================================
	/**
	 * 进入用户管理主界面
	 * 
	 * @return
	 */
	public String userAdminMain() {
		statisticsList = userService.getGroupByUserType();
		return SUCCESS;
	}

	/**
	 * 获取用户列表
	 * 
	 * @return
	 */
	public String queryUserList() {
		statisticsList = userService.getGroupByUserType();
		if (user == null) {
			user = new UserVO();
		}
		userList = userService.geteUserListByPage(user);
		return SUCCESS;
	}

	public String addUserPage() {
		statisticsList = userService.getGroupByUserType();
		return SUCCESS;
	}

	public String addingUser() {
		boolean exist = userService.isLoginidExist(user.getLoginid());
		if (exist) {
			message = user.getLoginid() + "已经存在,请选用其他登录名!";
		} else {
			try{
				userService.addUser(user);
				user = null;
				message = "用户"+user.getLoginid()+"添加成功,默认密码为123123";
			}catch(Exception e){
				logger.error("addingUser Error!",e);
				message = Constants.SYSTEM_ERROR_MSG;
			}
		}
		statisticsList = userService.getGroupByUserType();
		return SUCCESS;
	}

	public String doModifyPasswd() {
		UserVO sessionUser = userService.getUserInfoFromSession(session);
		int userid = sessionUser.getUserid();
		user.setUserid(userid);
		boolean result = userService.modifyPasswd(user);
		if (result) {
			message = "密码修改成功!";
		} else {
			message = "原密码错误,请重新输入!";
		}
		user = null;
		return SUCCESS;
	}

}
